/**
 * 应用数据持久化存储 
 * */
import util from "@/libs/util";
export default {
  namespaced: true,
  state: {
    // 应用基本数据
    details: {},
  },
  actions: {
    /**
     * @description 设置应用基本数据
     * @param {Object} state vuex state
     * @param {Object} dispatch vuex dispatch
     * @param {*} details details
     */
    set({ state, dispatch }, details) {
      return new Promise(async (resolve) => {
        // store 赋值
        state.details = details;
        util.cookies.set("sysCode", details["appCode"]);
        util.cookies.set("appId", details["appId"]);
        // 持久化
        await dispatch(
          "admin/db/set",
          {
            dbName: "sys",
            path: "apps.details",
            value: details,
            user: true,
          },
          { root: true }
        );
        // end
        resolve(null);
      });
    },
    /**
     * @description 从数据库取应用基本数据
     * @param {Object} state vuex state
     * @param {Object} dispatch vuex dispatch
     */
    load({ state, dispatch }) {
      return new Promise(async (resolve) => {
        // store 赋值
        state.details = await dispatch(
          "admin/db/get",
          {
            dbName: "sys",
            path: "apps.details",
            defaultValue: {},
            user: true,
          },
          { root: true }
        );
        // end
        resolve(null);
      });
    },
  },
};
